import {Message} from 'iview';
function reduceHandleEven(func: any): void {
    if (!func) {
        return;
    }
    else if (Object.prototype.toString.call(func) === '[object Function]') {
        func && func();
    }
    else if (func.constructor === Array || Object.prototype.toString.call(func) === '[object Array]') {
        func.map((item) => {
            if (typeof item === 'function') {
                item();
            }
        });
    }
}
interface MessageOption{
    message:string,
    error:string,
    state:any,
    even:Array<any>
}
const MessagePlugin={
    install:function(Vue,options){
        Vue.prototype.$MessagePlugin = {
            info: function (message: string, duration: number = 4, closable: boolean) {
                Message.info({ content: message, duration: duration || 4, closable: closable || false })
            },
            success(message: string, duration: number = 4, closable: boolean) {
                Message.success({ content: message, duration: duration || 2, closable: closable || false })
            },
            warn(message: string, duration: number = 4, closable: boolean) {
                Message.warning({ content: message, duration: duration || 4, closable: closable || false })
            },
            fail(message: string, duration: number = 4, closable: boolean) {
                Message.error({ content: message, duration: duration || 4, closable: closable || false })
            }
        }
        Vue.prototype.AutoMessage=function(option:MessageOption){
            option = option || {};
            option.message = option.message || 'success';
            option.error = option.error || 'fail';
            option.state = option.state || null;
            option.even = option.even || [];
            if (option.state && option.state.isResolved) {
                let state = option.state.data;
                let operationState = state && state.success;
                if (operationState) {
                    this.$MessagePlugin.success(option.message || state.message);
                    reduceHandleEven(option.even);
                }else{
                    this.$MessagePlugin.fail(state.message || option.error);
                }
            }
        }
        Vue.prototype.messageAlert=function (curVal, message, failMessage, funcArray) {
            if (failMessage && failMessage.constructor === Array) {
              funcArray = failMessage;
            }
            if (message && message.constructor === Array) {
              funcArray = message;
            }
            if (curVal.isResolved) {
              let result = curVal.data.success;
              if (result) {
                this.$MessagePlugin.success(message || curVal.data.message);
                reduceHandleEven(funcArray);
              }
              else {
                this.$MessagePlugin.fail(curVal.data.message || failMessage);
              }
            }
          }
    }
}
export default MessagePlugin